<?php
	//Connect to database
	require_once $_SERVER['DOCUMENT_ROOT'] . '/deviantpic/inc/database.php';
	
	//check arguments
	if (isset($_GET['action'])) { 
		if ($_GET['action'] == 'show_picture_comments') show_all_picture_comments();
		else if ($_GET['action'] == 'show_wall_comments') show_all_wall_comments();
		else if ($_GET['action'] == 'show_message_replies') show_all_replies();
	}
	
	function show_all_wall_comments() {
		$id = null;
		$user_id = null;
		
		if (!isset($_GET['wall_id'])) echo "";
		else {
			$id = $_GET['wall_id'];
		}
		
		if (!isset($_GET['user_id'])) echo "";
		else {
			$user_id = $_GET['user_id'];
		}
		
		//Get comments
		$comments = array();
		$comment_results	= mysql_query("SELECT * FROM wall_comments WHERE post_id=" . $id . " ORDER BY time ASC") or die(mysql_error());
		$comment_row		= mysql_fetch_array($comment_results);
		
		$index	= 0;
		$amount	= mysql_num_rows($comment_results);
	
		while ($comment_row && count($comments) < $amount) {
			//Add to post
			$comments[] = $comment_row;
			$comment_row = mysql_fetch_array($comment_results);
		}
		
		//For each comment
		foreach ($comments as $comment) {
			//Get poster name
			$result = mysql_query("SELECT * FROM users WHERE id=" . $comment['user_id']) or die(mysql_error());
			$poster = mysql_fetch_array($result);
?>
			<div class='wall-comment'>
				<li class="wall-comment">
				<!-- Comment header -->
				<div class='wall-comment-header'>
					<div class='wall-comment-user'> <?php echo $poster['username'] ?> </div>
					<div class='wall-comment-time'> <?php echo $comment['time'] ?> </div>
				</div>

			<!-- Comment content -->
			<div class='wall-comment-content'>
				<div class="comment-content"><?php echo $comment['content'] ?></div>
					<?php
						if (isset($_SESSION['user']) && is_numeric($_SESSION['user'])) {
							if ($_SESSION['user'] == $user_id) {
					?>
							<div class="comment-delete">
								<a href=<?php echo '"index.php?id=' . $user_id . "&del-comment=" . $comment['id'] . '"'; ?>>delete</a>
							</div>
					<?php
							}	
						}
					?>
				</div>
				</li>
			</div>
			<?php
			}
			?>
			<!-- Comment input -->
			<div class="wall-comment-form">
				<form action=<?php echo "'index.php?id=" . $user_id . "'" ?> method="post"> <p>
				<!-- Content input -->
				<textarea cols="32" rows="2" class="input-wall-comment" name="comment">Write comment</textarea>
				<br />

				<!-- Which wall post? -->
				<input type="hidden" name="post_id" value=<?php echo "'" . $id . "'" ?> />

				<!-- Button -->
				<input type="submit" value="Comment" />
				</p></form>
			</div>		
<?php 
	}
	
	function show_all_picture_comments() {
		$id = null;
		if (!isset($_GET['photo_id'])) echo "";
		else {
			$id = $_GET['photo_id'];
		}
		
		//load comments
		$start = 0;
		if (isset($_GET['start'])) $start = $_GET['start'];
	
		$comments = array();
		$comments_result = mysql_query("SELECT * FROM photo_comments WHERE photo_id=" . $id . " ORDER BY time DESC") or die (mysql_error());
		$comments_row	= mysql_fetch_array($comments_result);
		
		$index	= 0;
		$amount	= mysql_num_rows($comments_result);
		
		while ($comments_row && count($comments) < $amount) {
			//Starting from start
			if  ($index >= $start) {
				//Insert to photo
				$comments[] = $comments_row;
				$comments_row = mysql_fetch_array($comments_result);
			}
		}
		
		//For each comment
		foreach ($comments as $comment) {
			//Get poster name
			$result = mysql_query("SELECT * FROM users WHERE id=" . $comment['user_id']) or die(mysql_error());
			$poster = mysql_fetch_array($result);
?>
			<!-- Photo Comment -->
			<div class="photo-comment">
				<div class="photo-comment-header"> 
					<div class=photo-comment-poster> <?php echo $poster['username'] ?> </div>
					<div class=photo-comment-time> <?php echo $comment['time'] ?></div>
				</div>
				<div class="photo-comment-content"> 
					<?php echo $comment['content'] ?>
				</div>
			</div>
<?php 
		}
	}
	
	function show_all_replies() {
		$id = null;
		$message_id = null;
		
		if (!isset($_GET['message_id'])) echo "";
		else {
			$message_id = $_GET['message_id'];
		}
		
		if (!isset($_GET['user_id'])) echo "";
		else {
			$user_id = $_GET['user_id'];
		}
		
		//Prepare
		$messages	= array();

		//Get messages aimed at user
		$result = mysql_query("SELECT * FROM messages WHERE id=" . $message_id) or die(mysql_error());
		$row	= mysql_fetch_array($result);
	
		//Get message
		$result = mysql_query("SELECT * FROM messages WHERE thread=" .$row['thread']) or die(mysql_error());
		$row	= mysql_fetch_array($result);

		//While there's still message
		while ($row) {
			//Get sender name
			$sender_result		= mysql_query("SELECT username FROM users WHERE id=" . $row['sender_id']) or die(mysql_error());
			$sender_row			= mysql_fetch_array($sender_result);
			$row['sender']		= $sender_row['username'];
			$row['receivers']	= array();

			//Get receivetrs
			$receivers_result	= mysql_query("SELECT receiver_id FROM message_receivers WHERE message_id=" . $row['id']);
			$receivers_row		= mysql_fetch_array($receivers_result);
			while ($receivers_row) {
				//Get name
				$receiver_result	= mysql_query("SELECT username FROM users WHERE id=" . $receivers_row['receiver_id']);
				$receiver_row		= mysql_fetch_array($receiver_result);
				$row['receivers'][]	= $receiver_row['username'];

				//Next
				$receivers_row = mysql_fetch_array($receivers_result);
			}

			//Insert to message list
			$messages[] = $row;

			//Next
			$row = mysql_fetch_array($result);
		}
		
		//From 1 to the rest
		$i = 1;
		while ($i < count($messages)) {
?>
		<li class="message">
			<div class="message">
			<!-- Header: subject & time -->
			<div class="message-header">
				<div class="message-header-subject"><?php echo $messages[$i]['subject']; ?></div>
				<div class="message-header-time"><?php echo $messages[$i]['time']; ?></div>
			</div>

			<!-- Content -->
			<div class="message-content">
			<!-- Sender -->
			<div class="message-content-sender">
				<span class="message-from">From: </span>
				<span class="message-sender"><?php echo $messages[$i]['sender']; ?></span>;
			</div>

			<!-- Receivers -->
			<div class="message-content-receiver">
				<span class="message-to">To: </span>
				<span class="message-receivers">
				<?php
				foreach($messages[$i]['receivers'] as $receiver) {
				?>
					<span class="message-receiver"><?php echo $receiver; ?></span>;
				<?php
				}
				?>
					</span>
			</div>

			<!-- Content -->
			<div class="message-content-text">
				<span class="message-content-text"><?php echo $messages[$i]['content']; ?></span>
				<span class="message-reply">
				<a href= <?php echo '"message.php?id=' . $_SESSION['user'] . "&reply=" . $messages[$i]['id'] . '"'; ?>>reply</a>
				</span>
			</div>
			
			</div>
		
			</div>
		</li>
		<?php
		//Next
		$i++;
		}
	}
?>